package com.ruoyi.labdata.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.mapper.LabFlowNodeMapper;
import com.ruoyi.labdata.domain.LabFlowNode;
import com.ruoyi.labdata.service.ILabFlowNodeService;

/**
 * 流程节点Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-06
 */
@Service
public class LabFlowNodeServiceImpl implements ILabFlowNodeService {
	private static final String END = "End";
	private static final String START = "Start";
	@Autowired
	private LabFlowNodeMapper labFlowNodeMapper;

	/**
	 * 查询流程节点
	 * 
	 * @param nodeId
	 *            流程节点ID
	 * @return 流程节点
	 */
	@Override
	public LabFlowNode selectLabFlowNodeById(Long nodeId) {
		return labFlowNodeMapper.selectLabFlowNodeById(nodeId);
	}
	

	/**
	 * 查询流程节点列表
	 * 
	 * @param labFlowNode
	 *            流程节点
	 * @return 流程节点
	 */
	@Override
	public List<LabFlowNode> selectLabFlowNodeList(LabFlowNode labFlowNode) {
		return labFlowNodeMapper.selectLabFlowNodeList(labFlowNode);
	}

	/**
	 * 新增流程节点
	 * 
	 * @param labFlowNode
	 *            流程节点
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabFlowNode(LabFlowNode labFlowNode) {
		LabFlowNode endNode = labFlowNodeMapper.getLabFlowNodeByworkflowIdAndNodeName(labFlowNode.getWorkflowId(), "end");
		if(endNode==null){
			throw new RuntimeException("不能获取到该流程的结束节点,流程Id:"+labFlowNode.getWorkflowId());
		}
		labFlowNode.setNodeNo(endNode.getNodeNo());
		endNode.setNodeNo(endNode.getNodeNo()+1);
		this.labFlowNodeMapper.updateLabFlowNode(endNode);
		int key = labFlowNodeMapper.insertLabFlowNode(labFlowNode);
		if(key==0){
			throw new RuntimeException("该流程节点序号插入冲突，请稍后再试!");
		}
		//labFlowNode.setStatusOpt("1");
		//this.labFlowNodeMapper.updateLabFlowNode(labFlowNode);
		return key;
	}

	/**
	 * 修改流程节点
	 * 
	 * @param labFlowNode
	 *            流程节点
	 * @return 结果
	 */
	@Override
	public int updateLabFlowNode(LabFlowNode labFlowNode) {
		if(labFlowNode.getNodeName().equals(START) || labFlowNode.getNodeName().equals(END)){
			return 0;
		}
		int key = labFlowNodeMapper.updateLabFlowNode(labFlowNode);
		if(key>0){
			List<LabFlowNode> nodeList = getAllFlowNodeByWorkflowId(labFlowNode.getWorkflowId());
			for(LabFlowNode itemNode : nodeList){
				if(itemNode.getNodeNo()>labFlowNode.getNodeNo()){
					itemNode.setNodeNo(itemNode.getNodeNo() - 1);
					this.updateLabFlowNode(itemNode);
				}
			}
		}
		return key;
	}

	/**
	 * 修改流程节点
	 * 
	 * @param labFlowNode
	 *            流程节点
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabFlowNodeNo(List<LabFlowNode> labFlowNodeList) {
		if(labFlowNodeList.size()<=2){
			return 0;
		}
		LabFlowNode startNode = this.labFlowNodeMapper.selectLabFlowNodeById(labFlowNodeList.get(0).getNodeId());
		if(!startNode.getNodeName().equals(START) ){
			throw new RuntimeException("流程第一个节点必须是开始节点:Start.");
		}
		LabFlowNode endNode = this.labFlowNodeMapper.selectLabFlowNodeById(labFlowNodeList.get(labFlowNodeList.size() -1).getNodeId());
		if(!endNode.getNodeName().equals(END) ){
			throw new RuntimeException("流程最后的节点必须是结束节点:End.");
		}
		for (int i=0; i<labFlowNodeList.size(); i++) {
			LabFlowNode labFlowNode = labFlowNodeList.get(i);
			labFlowNode.setNodeNo(i+1);
			labFlowNodeMapper.updateLabFlowNode(labFlowNode);
		}
		return 1;
	}

	/**
	 * 批量删除流程节点
	 * 
	 * @param nodeIds
	 *            需要删除的流程节点ID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabFlowNodeByIds(Long[] nodeIds) {
		return labFlowNodeMapper.deleteLabFlowNodeByIds(nodeIds);
	}*/

	/**
	 * 删除流程节点信息
	 * 
	 * @param nodeId
	 *            流程节点ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabFlowNodeById(Long nodeId) {
		LabFlowNode node = this.labFlowNodeMapper.selectLabFlowNodeById(nodeId);
		if(node == null){
			throw new RuntimeException("不能查找到流程节点，节点Id:"+nodeId);
		}
		if(node.getNodeName().equals(START) || node.getNodeName().equals(END)){
			throw new RuntimeException("流程的开始节点或结束节点不能被删除");
		}
		int key= labFlowNodeMapper.deleteLabFlowNodeById(nodeId);
		if(key>0){
			List<LabFlowNode> nodeList = getAllFlowNodeByWorkflowId(node.getWorkflowId());
			for(LabFlowNode itemNode : nodeList){
				if(itemNode.getNodeNo()>node.getNodeNo()){
					itemNode.setNodeNo(itemNode.getNodeNo() - 1);
					this.updateLabFlowNode(itemNode);
				}
			}
		}
		return key;
	}

	public List<LabFlowNode> getAllFlowNodeByWorkflowId(Long workflowId) {
		return labFlowNodeMapper.getAllFlowNodeByWorkflowId(workflowId);
	}
	
	 public String getUserIdsByRole(String roleIds){
		 StringBuffer userIdBuffer= new StringBuffer();
		 List<Long> userIdList = labFlowNodeMapper.getUserIdByRole(roleIds);
		 int count =0;
		 for(Long userId : userIdList){
			 if(count>0){
				 userIdBuffer.append(",");
			 }
			 userIdBuffer.append(userId);
			 count ++;
		 }
		 return userIdBuffer.toString();
	 }
	    
	  public String getUserIdsByDeptRole(String roleIds, String ancestors, Long deptId){
		  StringBuffer userIdBuffer= new StringBuffer();
		  List<Long> userIdList = labFlowNodeMapper.getUserIdByDeptRole(roleIds, ancestors, deptId);
		 int count =0;
		 for(Long userId : userIdList){
			 if(count>0){
				 userIdBuffer.append(",");
			 }
			 userIdBuffer.append(userId);
			 count ++;
		 }
		 return userIdBuffer.toString();
	  }
}
